#include "addwindow.h"
#include "ui_addwindow.h"
#include "mytcpsocket.h"
#include "mytablewidgetitem.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonArray>
#include <QTableWidget>
#include <QTableWidgetItem>

AddWindow::AddWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddWindow)
{
    ui->setupUi(this);
    // qss渲染
    setQssStyle(QString("../style/AddWindow.qss"),this);
    // 无边框
    this->setWindowFlags(Qt::FramelessWindowHint);

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//根据表格自适应列宽的大小
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);//根据表格自适应列高的大小
    ui->tableWidget->verticalHeader()->setHidden(true); //隐藏行标题
    ui->tableWidget->horizontalHeader()->setHidden(true);//隐藏列标题

    connect(this,SIGNAL(sig_searchResult(QVariant)),this,SLOT(slot_searchResult(QVariant)));
    connect(this,SIGNAL(sig_searchGroupResult(QVariant)),this,SLOT(slot_searchGroupResult(QVariant)));

    QStringList sex;
    sex<<"不限"<<"男"<<"女";
    ui->cb_sex->addItems(sex);

    QStringList age;
    age<<"不限"<<"18-22岁"<<"23-26岁"<<"27-35岁"<<"35岁以上";
    ui->cb_age->addItems(age);

    QStringList home;
    home<<"不限"<<"湖南"<<"福建"<<"广东"<<"上海"<<"浙江";
    ui->cb_home->addItems(home);
}

AddWindow::~AddWindow()
{
    delete ui;
}

bool AddWindow::isInt(QString str)
{
    bool isInt;
    str.toInt(&isInt);
    return isInt;
}

void AddWindow::on_btn_find_clicked()
{
    //获取用户输入
    QString user_input = ui->le_search->text();
    // 性别
    QString sex = ui->cb_sex->currentText();
    if(sex != "男" && sex != "女")
        sex = "不限";

    //找人
    if(ui->rbtn_friend->isChecked()){
        QVariantMap map;
        //判断用户输入是否是整数
        if(isInt(user_input)){  // 纯整数,则认为用户输入的是 DD号
            //封装数据请求
            map["cmd"]="get";
            map["type"]="user_info";
            QJsonObject obj;
            QJsonArray arr;
            obj.insert("field","dd_num");
            obj.insert("text",user_input.toInt());  //dd_num的值一定要转为int,不然服务器那边段错误
            obj.insert("sex",sex);
            arr.insert(0,obj);
            map["condition"]=arr;

        }else{              //不是整数,则认为用户输入的是 昵称
            //封装数据请求
            map["cmd"]="get";
            map["type"]="user_info";
            QJsonObject obj;
            QJsonArray arr;
            obj.insert("field","nick");
            obj.insert("text",user_input);
            obj.insert("sex",sex);
            arr.insert(0,obj);
            map["condition"]=arr;
        }
        //发送请求
        GlobalObject::getGlobalObject()->sendRequest(map);
    }
    //找群
    else if(ui->rbtn_group->isChecked()){
        QVariantMap map;
        //判断用户输入是否是整数
        if(isInt(user_input)){  // 纯整数,则认为用户输入的是 群ID
            //封装数据请求
            map["cmd"]="get";
            map["type"]="group_info";
            QJsonObject obj;
            QJsonArray arr;
            obj.insert("field","gid");
            obj.insert("text",user_input.toInt());  //gid的值一定要转为int,不然服务器那边段错误
            arr.insert(0,obj);
            map["condition"]=arr;

        }
        //不是整数,则认为用户输入的是 群昵称
        else{
            //封装数据请求
            map["cmd"]="get";
            map["type"]="group_info";
            QJsonObject obj;
            QJsonArray arr;
            obj.insert("field","gname");
            obj.insert("text",user_input);
            arr.insert(0,obj);
            map["condition"]=arr;
        }
        //发送请求
        GlobalObject::getGlobalObject()->sendRequest(map);
    }
}

void AddWindow::slot_searchResult(QVariant var)
{
    //QVariant -> struct st_searchUserResult
    m_searchRes = var.value<struct st_searchUserResult>();
    if(m_searchRes.res == 0){  //无结果
        ui->tableWidget->clear();
        ui->tableWidget->setRowCount(1);// 设置表格行数
        ui->tableWidget->setColumnCount(1);// 设置表格列数
        QTableWidgetItem* item = new QTableWidgetItem("未查询到相关信息");
        item->setTextAlignment(Qt::AlignCenter);    //文本居中
        item->setFont(QFont("楷体", 14));     //单元格设置字体
        ui->tableWidget->setItem(0,0,item);
    }else{    //有结果
        ui->tableWidget->clear();
        //获取查询结果的个数
        int count = m_searchRes.friend_list.size();
        ui->tableWidget->setRowCount(2);// 设置表格行数
        ui->tableWidget->setColumnCount(2);// 设置表格列数

        int m = 0;
        int n = 0;
        for(int i = 0;i<count;i++){
            st_friendData fdata =  m_searchRes.friend_list[i];
            MyTableWidgetItem* myitem = new MyTableWidgetItem(fdata,ui->tableWidget);
            QTableWidgetItem* item = new QTableWidgetItem;
            if(i<2){
                ui->tableWidget->setItem(0,m,item);
                ui->tableWidget->setCellWidget(0,m,myitem);
                m++;
            }else{
                m = 1;
                ui->tableWidget->setItem(m,n,item);
                ui->tableWidget->setCellWidget(m,n,myitem);
                n++;
            }

        }
    }
}

void AddWindow::slot_searchGroupResult(QVariant var)
{
    //QVariant -> struct st_searchGroupResult
    m_searchgroupRes = var.value<struct st_searchGroupResult>();
    if(m_searchgroupRes.res == 0){  //无结果
        ui->tableWidget->clear();
        ui->tableWidget->setRowCount(1);// 设置表格行数
        ui->tableWidget->setColumnCount(1);// 设置表格列数
        QTableWidgetItem* item = new QTableWidgetItem("未查询到相关信息");
        item->setTextAlignment(Qt::AlignCenter);    //文本居中
        item->setFont(QFont("楷体", 14));     //单元格设置字体
        ui->tableWidget->setItem(0,0,item);
    }else{    //有结果
        ui->tableWidget->clear();
        //获取查询结果的个数 群 2*2
        int count = m_searchgroupRes.group_list.size();
        ui->tableWidget->setRowCount(2);// 设置表格行数
        ui->tableWidget->setColumnCount(2);// 设置表格列数

        int m = 0;
        int n = 0;
        for(int i = 0;i<count;i++){
            st_gData gdata =  m_searchgroupRes.group_list[i];
            MyTableWidgetItem* myitem = new MyTableWidgetItem(gdata,ui->tableWidget);
            QTableWidgetItem* item = new QTableWidgetItem;       
            if(i<2){
                ui->tableWidget->setItem(0,m,item);
                ui->tableWidget->setCellWidget(0,m,myitem);
                m++;
            }else{
                m = 1;
                ui->tableWidget->setItem(m,n,item);
                ui->tableWidget->setCellWidget(m,n,myitem);
                n++;
            }
        }
    }
}


void AddWindow::on_btn_close_clicked()
{
    this->close();
}


void AddWindow::on_btn_min_clicked()
{
    this->showMinimized();
}


void AddWindow::on_rbtn_friend_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_user);
}


void AddWindow::on_rbtn_group_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_group);
}

